package com.ejlchina.tool.fragment

import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import com.adolf.androiddemo.R
import com.adolf.androiddemo.databinding.FragmentInputSignalBinding
import com.huyuhui.fastble.data.BleDevice
import com.ejlchina.tool.adapter.DeviceAdapterRadio
import com.ejlchina.tool.base.BaseBindingFragment2
import com.ejlchina.tool.bean.BleDeviceBean
import com.ejlchina.tool.bean.NpnInputBean
import com.ejlchina.tool.enums.NpnEnum
import com.ejlchina.tool.singleton.GlobalConnectObject
import com.ejlchina.tool.util.COMMAND_NPN_SIGNAL_INPUT_GET_DOWNWARD
import com.ejlchina.tool.util.COMMAND_NPN_SIGNAL_INPUT_GET_UPWARD
import com.ejlchina.tool.util.COMMAND_NPN_SIGNAL_INPUT_SET_DOWNWARD
import com.ejlchina.tool.util.COMMAND_NPN_SIGNAL_INPUT_SET_UPWARD
import com.ejlchina.tool.util.LOG_TAG
import com.ejlchina.tool.util.bytearray.intTo32HexStringLE
import com.ejlchina.tool.util.bytearray.readByteArrayLE
import com.ejlchina.tool.util.getBitValueFrom32Bits
import com.ejlchina.tool.util.setBitValue
import com.ejlchina.tool.util.showToast
import com.ejlchina.tool.view.GridItemDecoration

class InputSignalFragment: BaseBindingFragment2<FragmentInputSignalBinding>() {
    
    private val inputSignalBean = NpnInputBean()
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViews()
        initData()
        
    }
    
    private fun initViews(){
        setHeadTitle("输入信号")
        setRightIconVisibility(View.GONE)
        initListeners()
    }

    private fun initData(){
        initDevices()
    }

    private fun initDevices(){
        deviceAdapter = DeviceAdapterRadio(GlobalConnectObject.getCurrentConnectDeviceList()){
            getInputSignalData(it)
        }
        binding.rvDevices.apply {
            adapter = deviceAdapter
            layoutManager = GridLayoutManager(requireContext(),3)
            if (itemDecorationCount == 0){
                addItemDecoration(GridItemDecoration(3,11,11))
            }
        }
    }

    private fun getResultData(): String{
        var npnValue = 0
        if (!inputSignalBean.smoke){
            npnValue = setBitValue(npnValue,0)
        }
        if (!inputSignalBean.full){
            npnValue = setBitValue(npnValue,1)
        }
        if (!inputSignalBean.body){
            npnValue = setBitValue(npnValue,2)
        }
        if (!inputSignalBean.button){
            npnValue = setBitValue(npnValue,3)
        }
        if (!inputSignalBean.lock){
            npnValue = setBitValue(npnValue,4)
        }
        if (!inputSignalBean.reuse){
            npnValue = setBitValue(npnValue,5)
        }
        if (!inputSignalBean.machineAntiPinch){
            npnValue = setBitValue(npnValue,6)
        }
        if (!inputSignalBean.reserveInput){
            npnValue = setBitValue(npnValue,7)
        }
        return intTo32HexStringLE(npnValue)
    }

    private fun setSignalInputData(bleDevice: BleDeviceBean){
        setCommandData(bleDevice, COMMAND_NPN_SIGNAL_INPUT_SET_DOWNWARD,getResultData())
    }

    private fun initListeners(){

        binding.btnConfirm.setOnClickListener {
            setSignalInputData(deviceAdapter.getSelectDevice())
        }

        binding.msSmoke.setTurnOnAndOff(onBlock = {
            inputSignalBean.smoke = true
        }, offBlock = {
            inputSignalBean.smoke = false
        })

        binding.msFull.setTurnOnAndOff(onBlock = {
            inputSignalBean.full = true
        }, offBlock = {
            inputSignalBean.full = false
        })

        binding.msHuman.setTurnOnAndOff(onBlock = {
            inputSignalBean.body = true
        }, offBlock = {
            inputSignalBean.body = false
        })

        binding.msButton.setTurnOnAndOff(onBlock = {
            inputSignalBean.button = true
        }, offBlock = {
            inputSignalBean.button = false
        })

        binding.msLock.setTurnOnAndOff(onBlock = {
            inputSignalBean.lock = true
        }, offBlock = {
            inputSignalBean.lock = false
        })

        binding.msReuse.setTurnOnAndOff(onBlock = {
            inputSignalBean.reuse = true
        }, offBlock = {
            inputSignalBean.reuse = false
        })

        binding.msAntiPinch.setTurnOnAndOff(onBlock = {
            inputSignalBean.machineAntiPinch = true
        }, offBlock = {
            inputSignalBean.machineAntiPinch = false
        })

        binding.msReserve.setTurnOnAndOff(onBlock = {
            inputSignalBean.reserveInput = true
        }, offBlock = {
            inputSignalBean.reserveInput = false
        })

    }

    override fun whenReceiveCommand(bleDevice: BleDeviceBean, command: String, commandByteArray: ByteArray) {
        super.whenReceiveCommand(bleDevice,command, commandByteArray)
        Log.i(LOG_TAG,"接收指令信息")
        when (command){
            COMMAND_NPN_SIGNAL_INPUT_GET_UPWARD -> {
                setInputSingleBeanData(commandByteArray.readByteArrayLE(0,commandByteArray.size))
                setUIData()
            }
            COMMAND_NPN_SIGNAL_INPUT_SET_UPWARD -> {
                val resultInt = commandByteArray[4].toInt()
                val toastString = if (resultInt == 1) "设置成功" else "设置失败"
                showToast(toastString)
            }
        }
        dismissLoading()
    }

    private fun setInputSingleBeanData(dataByteArray: ByteArray){
        inputSignalBean.smoke = getBitValueFrom32Bits(dataByteArray,0) == NpnEnum.OPEN.value
        inputSignalBean.full = getBitValueFrom32Bits(dataByteArray,1) == NpnEnum.OPEN.value
        inputSignalBean.body = getBitValueFrom32Bits(dataByteArray,2) == NpnEnum.OPEN.value
        inputSignalBean.button = getBitValueFrom32Bits(dataByteArray,3) == NpnEnum.OPEN.value
        inputSignalBean.lock = getBitValueFrom32Bits(dataByteArray,4) == NpnEnum.OPEN.value
        inputSignalBean.reuse = getBitValueFrom32Bits(dataByteArray,5) == NpnEnum.OPEN.value
        inputSignalBean.machineAntiPinch = getBitValueFrom32Bits(dataByteArray,6) == NpnEnum.OPEN.value
        inputSignalBean.reserveInput = getBitValueFrom32Bits(dataByteArray,7) == NpnEnum.OPEN.value
    }

    private fun setUIData(){
        binding.msSmoke.setState(inputSignalBean.smoke)
        binding.msFull.setState(inputSignalBean.full)
        binding.msHuman.setState(inputSignalBean.body)
        binding.msButton.setState(inputSignalBean.button)
        binding.msLock.setState(inputSignalBean.lock)
        binding.msAntiPinch.setState(inputSignalBean.machineAntiPinch)
        binding.msReuse.setState(inputSignalBean.reuse)
        binding.msReserve.setState(inputSignalBean.reserveInput)
    }


    private fun getInputSignalData(bleDevice: BleDeviceBean){
        getCommandData(bleDevice,COMMAND_NPN_SIGNAL_INPUT_GET_DOWNWARD)
    }
    override fun getViewBinding(inflater: LayoutInflater, container: ViewGroup): FragmentInputSignalBinding {
        return FragmentInputSignalBinding.inflate(inflater,container,true)
    }
}